package com.doublefish.doublefishrobot.ui.home.s5;

import android.app.Dialog;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.doublefish.doublefishrobot.MainActivity;
import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.base.BaseActivity;
import com.doublefish.doublefishrobot.ui.home.bean.DeviceBean;
import com.doublefish.doublefishrobot.utils.ble.bean.ModeConstant;
import com.doublefish.doublefishrobot.utils.socket.DFS5ProtocolMaker;
import com.doublefish.doublefishrobot.utils.socket.SocketManager;
import com.doublefish.doublefishrobot.widget.CommonDialog;

import butterknife.BindView;
import butterknife.OnClick;

public class S5ModeMenuActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView titleTV;

    @BindView(R.id.ll_unload)
    LinearLayout unloadLL;

    @Override
    protected int getLayoutId() {
        return R.layout.activty_s5_menu;
    }

    @Override
    protected void initView() {
        DeviceBean deviceBean = SocketManager.getInstance().getCurDeviceBean();
        if (deviceBean != null) {
            String title = deviceBean.getAlias();
            if (!title.isEmpty()) {
                titleTV.setText(title);
            }

            if (deviceBean.getDeviceType() == ModeConstant.DeviceTypeS3) {
                unloadLL.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        SocketManager.getInstance().setOnDisConnectListner(new SocketManager.OnSocketDisConnectListner() {
            @Override
            public void onDisConnect() {
                toMainActivity();
            }
        });
    }

    private void toMainActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
    }

    private void toS5ModeNormalActivity() {
        Intent intent = new Intent(this, S5ModeNormalActivity.class);
        startActivity(intent);
    }

    private void toS5ModeRandomListActivity() {
        Intent intent = new Intent(this, S5ModeRandomListActivity.class);
        startActivity(intent);
    }

    private void toS5ModeTrainListActivity() {
        Intent intent = new Intent(this, S5ModeTrainListActivity.class);
        startActivity(intent);
    }

    private void toS5ModeCustomListActivity() {
        Intent intent = new Intent(this, S5ModeCustomListActivity.class);
        startActivity(intent);
    }

    @OnClick({R.id.tv_mode_normal, R.id.tv_mode_random, R.id.tv_mode_train, R.id.tv_mode_custom, R.id.tv_unload, R.id.tv_unbind_remote})
    public void onItemClick(View view) {
        switch (view.getId()) {
            case R.id.tv_mode_normal:
                toS5ModeNormalActivity();
                break;
            case R.id.tv_mode_random:
                toS5ModeRandomListActivity();
                break;
            case R.id.tv_mode_train:
                toS5ModeTrainListActivity();
                break;
            case R.id.tv_mode_custom:
                toS5ModeCustomListActivity();
                break;

            case R.id.tv_unload:
                showUnloadDialog();
                break;

            case R.id.tv_unbind_remote:
                showUnbindRemoteDialog();
                break;

        }
    }

    @OnClick(R.id.ll_back)
    public void onBackClick() {
        showBackAlert();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            showBackAlert();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void showBackAlert() {
        //询问连接新设备
        String title = getString(R.string.disconnect_alert);
        CommonDialog dialog = new CommonDialog(this, title, null, new CommonDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    disconnectWifi();
                    finish();
                }
            }
        });
        dialog.setPositiveButton(getString(R.string.disconnect));
        dialog.show();
    }

    private void disconnectWifi() {
        SocketManager.getInstance().disConnectSocket();
    }

    //region 卸球
    private int unloadState = 0;  //0.默认状态 1.当前正在卸球
    private void showUnloadDialog() {
        unloadState = 0;
        String title = getString(R.string.unload_alert);
        String content = getString(R.string.unload_alert_2);
        CommonDialog dialog = new CommonDialog(this, title, content, new CommonDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (unloadState == 0) {
                    // 默认状态
                    if (confirm) {
                        CommonDialog dialog1 = (CommonDialog)dialog;
                        //修改UI
                        String title2 = getString(R.string.stop_unload_alert);
                        String content2 = getString(R.string.stop_unload_alert_2);
                        dialog1.setTitle(title2);
                        dialog1.setContent(content2);
                        dialog1.setPositiveButton(getString(R.string.stop_unload));
                        dialog1.setShowNegativeButton(false);
                        dialog1.refreshUI();

                        //开始卸球
                        unloadState = 1;
                        startUnload();
                    } else {
                        dialog.dismiss();
                    }
                } else {
                    // 当前正在卸球
                    dialog.dismiss();
                    //停止卸球
                    unloadState = 0;
                    stopUnload();
                }
            }
        });
        dialog.setPositiveButton(getString(R.string.unload));
        dialog.setAutoDismissWhenClick(false);
        dialog.show();
    }

    //开始卸球
    private void startUnload() {
        char[] unloadData = DFS5ProtocolMaker.unload();
        SocketManager.getInstance().sendData(unloadData);
    }

    //停止卸球
    private void stopUnload() {
        char[] stopUnloadData = DFS5ProtocolMaker.stopUnload();
        SocketManager.getInstance().sendData(stopUnloadData);
    }

    //endregion


    //region 遥控器解绑
    private void showUnbindRemoteDialog() {
        String title = getString(R.string.unbind_remote_control);
        String content = getString(R.string.unbind_remote_alert);
        CommonDialog dialog = new CommonDialog(this, title, content, new CommonDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    unbindRemote();
                }
            }
        });
        dialog.setPositiveButton(getString(R.string.unbind_2));
        dialog.show();
    }

    //遥控器解绑
    private void unbindRemote() {
        char[] unbindData = DFS5ProtocolMaker.unbindRemote();
        SocketManager.getInstance().sendData(unbindData);
    }

    //endregion
}

